package com.example.test2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.test2.R;
import com.example.test2.db.entity.User;
import com.example.test2.utils.PhoneUtils;

import java.util.ArrayList;
import java.util.List;

public class  FriendAdapter extends RecyclerView.Adapter<FriendAdapter.ViewHolder> {
    private Context context;
    private List<User> list = new ArrayList<>();
    private PhoneUtils screenUtils;

    public FriendAdapter(Context context, List<User> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_friend, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        screenUtils= PhoneUtils.getInstance();
        User user=list.get(position);
        if (user.getBoy()){
            holder.ivImg.setImageResource(R.drawable.ic_boy);
        }else {
            holder.ivImg.setImageResource(R.drawable.ic_girl);
        }
        holder.tvMsg.setText(user.getMsg());
        holder.tvPhone.setText(user.getPhone());
        holder.tvDate.setText(user.getDate());

        String path=user.getPath();
        if (!TextUtils.isEmpty(path)){
            holder.rlView.setVisibility(View.VISIBLE);
            showImage(holder.ivIcon,list.get(position).getPath());
        }else {
            holder.rlView.setVisibility(View.GONE);
        }
    }
    private void showImage(ImageView view, String path) {
        if (TextUtils.isEmpty(path)){
            return;
        }

        ViewGroup.LayoutParams lp = view.getLayoutParams();
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        //获取图片宽高
        double height = bitmap.getHeight();
        double width = bitmap.getWidth();

        if (height > width) {
            lp.height = screenUtils.dip2px(context, screenUtils.getScreenWidth(context));
            lp.width = (int) (width / height * lp.height);
        } else {
            lp.width = screenUtils.dip2px(context, screenUtils.getScreenWidth(context));
            lp.height = (int) (height / width * lp.width);
        }
        view.setLayoutParams(lp);
        view.setImageBitmap(bitmap);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rlView;
        private TextView tvPhone;
        private TextView tvDate;
        private ImageView ivIcon;
        private ImageView ivImg;
        private TextView tvMsg;

        public ViewHolder(View itemView) {
            super(itemView);
            rlView = itemView.findViewById(R.id.rl_view);
            tvMsg= itemView.findViewById(R.id.tv_msg);
            tvPhone=itemView.findViewById(R.id.tv_name);
            tvDate= itemView.findViewById(R.id.tv_date);
            ivIcon = itemView.findViewById(R.id.iv_pic);
            ivImg = itemView.findViewById(R.id.iv_img);
        }
    }

}
